/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：BLGAudio.java <br>
 * 创建时间：2014-8-13 上午11:36:54 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.blgframework.impl;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;

import com.orhanobut.logger.Logger;

import java.io.IOException;

import lostland.gmud.exv2.blgframework.Audio;
import lostland.gmud.exv2.blgframework.Music;
import lostland.gmud.exv2.blgframework.Sound;

/**
 * 类名：BLGAudio <br>
 * 说明：
 *
 * @author 12548
 */
public class BLGAudio implements Audio, OnLoadCompleteListener {

	AssetManager assets;
	SoundPool soundPool;

	/**
	 *
	 */
	@SuppressWarnings("deprecation")
	public BLGAudio(Activity activity) {
		activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		assets = activity.getAssets();
		soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
		soundPool.setOnLoadCompleteListener(this);
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.Audio#newMusic(java.lang.String)
	 */
	@Override
	public Music newMusic(String filename) {
		try {
			AssetFileDescriptor assetDescriptor = assets.openFd(filename);
			return new BLGMusic(assetDescriptor);
		} catch (IOException e) {
			return null;
		}
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.Audio#newSound(java.lang.String)
	 */
	@Override
	public Sound newSound(String filename) {
		try {
			AssetFileDescriptor assetDescriptor = assets.openFd(filename);
			int soundId = soundPool.load(assetDescriptor, 0);

			return new BLGSound(soundPool, soundId);
		} catch (IOException e) {
			throw new RuntimeException("Couldn't load sound '" + filename + "'");
		}
	}

	/* （非 Javadoc）
	 * @see android.media.SoundPool.OnLoadCompleteListener#onLoadComplete(android.media.SoundPool, int, int)
	 */
	@Override
	public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
		Logger.v("LoadComplete:" + sampleId + " of status:" + status);
	}

}
